package com.wangtao.rpc.netty;

import com.wangtao.rpc.prodiver.HelloServieImpl;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class NettyServerHandler extends ChannelInboundHandlerAdapter{

    //当有数据读取时
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("服务端接收到消息:" + msg);

        if (msg.toString().startsWith("HelloService#Hello#")) {
            //满足条件调用本地方法
            String message = new HelloServieImpl().sayHello(msg.toString().substring(msg.toString().lastIndexOf("#") + 1));
            //向客户端发送调用方法结果
            ctx.writeAndFlush(message);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        //有异常发送关闭通道
        System.out.println(cause.getMessage());
        ctx.close();
    }
}
